package com.bop.dehong.setting;

import java.util.TreeMap;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;

import com.base.dehong.http.RequestCallBack;
import com.base.dehong.util.RegexChk;
import com.base.dehong.util.ToastUtils;
import com.bop.dehong.R;
import com.bop.dehong.base.BaseActivity;
import com.bop.dehong.base.DeHongApp;
import com.bop.dehong.base.TopManager;
import com.bop.dehong.config.Config;
import com.bop.dehong.http.model.JudgeIdCardModel;
import com.bop.dehong.info.EmptyInfo;
import com.bop.dehong.util.WebUrlUtil;
import com.bop.dehong.webview.WebViewActivity;

/**
 *实名认证
 * @author TangWenhan/YeFengyun
 *2015-11-20下午7:21:25
 */
public class NameAuthActivity extends BaseActivity{
	private EditText mNameEt;
	private EditText mIdEt;
	private JudgeIdCardModel judgeIdCardModel;
	private String name;
	private String id;
	private String uid;

	@Override
	protected void initTop() {
		View mView = getView(R.id.top);
		TopManager manager = new TopManager(mView);
		manager.setTitle("实名认证");
		manager.setLeftImageVisibility(View.VISIBLE);
	}

	@Override
	protected void initUI() {
		mNameEt = getView(R.id.name);
		mIdEt = getView(R.id.id);
		registerOnClickListener(getView(R.id.submit_btn));
	}	
	
	@Override
	protected void initData() {
		uid = getIntent().getStringExtra(Config.UID);
	}
	
	public void submit(){
		name = getStr4TextView(mNameEt);
		id = getStr4TextView(mIdEt);
		if (TextUtils.isEmpty(name)) {
			ToastUtils.show(this, "姓名不能为空!");
			return;
		}
		if (!RegexChk.isChinese(name)) {
			ToastUtils.show(this, "姓名只能为中文，请重新输入！");
			return;
		}
		if (TextUtils.isEmpty(id)) {
			ToastUtils.show(this, "身份证不能为空!");
			return;
		}
		if (!RegexChk.checkIdCard(id)) {
			ToastUtils.show(this, "身份证格式不对，请重新输入！");
			return;
		}
		showProgressDialog();
		judgeIdCardModel = new JudgeIdCardModel(this, callback);
		judgeIdCardModel.doNet(uid, id);
	}
	
	RequestCallBack<EmptyInfo> callback = new RequestCallBack<EmptyInfo>() {

		@Override
		public void end() {
			disappearProgressDialog();
		}

		@Override
		public void onSuccess(EmptyInfo info) {
			super.onSuccess(info);
			Intent intent = new Intent(NameAuthActivity.this,WebViewActivity.class);
			intent.putExtra("title", "实名认证");
			intent.putExtra("url", getUrl(uid, name, id));
			intent.putExtra(Config.GO_BACK, true);
			startActivityForResult(intent, 100);
		}
		
	};
	
	private String getUrl(String uid, String name ,String id){
		TreeMap<String, String> postValues = new TreeMap<String, String>();
		postValues.put("a", "certupda");
		postValues.put("idcard", id);
		postValues.put("m", "User");
		postValues.put("name", name);
		postValues.put("uid", uid);
		return WebUrlUtil.getWebUrl(this, Config.getIP() + Config.nameAuth, postValues);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode !=  RESULT_OK) {
			return ;
		}
		if (requestCode == 100) {
			ToastUtils.show(this, "认证成功！");
			DeHongApp.mLoginEntity.certification = "2";
			if (DeHongApp.mUserInfo != null) {
				DeHongApp.mUserInfo.certification = "2";
			}
			setResult(RESULT_OK);
			finishCurrentActivity();
		}
	}

	@Override
	protected int getContentViewId() {
		TAG = getClass().getName();
		return R.layout.act_name_auth;
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId()) {
		case R.id.submit_btn:
			submit();
			break;

		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id)
	{
		// TODO Auto-generated method stub
		
	}

}

